#include"business.h"
#include<shadow.h>
#include <crypt.h>
int clientbusiness(int sockfd,const char* ip,const char* port){

    //密码验证
    char username[256] = {0};
    int ret = pwdAuth(sockfd,username);
    //printf("ret = %d\n",ret);

    //创建储存文件的目录
    mkdir("netDisk",0777);
    //perror("mkdir");

    while(1){
        clientExecCommand(sockfd,"reminder",ip,port,username);
        char command[4096] = {0};
        char ch;
        for(int i=0;i < 4096;i++)
        {
            ch = getchar();
            if(ch == '\n'){
                command[i] = '\0';
                break;
            }
            command[i] = ch;
        }
        //printf("command = %s\n",command);
        if(memcmp(command,"exit",strlen("exit"))==0){
            break;
        }
        clientExecCommand(sockfd,command,ip,port,username);
    }
}

int pwdAuth(int sockfd,char* userName){
    train_t train;
    while(1){
        //接收用户名和密码
       char username[256]={0};
       char password[256]={0};
       printf("Please enter your username:");
       scanf("%s",username);
       getchar();//接收最后的换行符;
       printf("Please enter your password:");
       scanf("%s",password);
       getchar();//接收最后的换行符

       memcpy(userName,username,strlen(username));

        //char username[256] = "test1";
        //char password[256] = "123456";

        //将用户名发送给服务端
        memset(&train,0,sizeof(train_t));
        train.length = strlen(username);
        memcpy(train.data,username,train.length);
        send(sockfd,&train.length,sizeof(train.length),0);
        send(sockfd,&train.data,train.length,0);
        //接收服务端返回的值
        memset(&train,0,sizeof(train_t));
        recvn(sockfd,&train.length,sizeof(train.length));
        recvn(sockfd,train.data,train.length);
        if(memcmp(train.data,"-1",strlen("0"))==0){
            //服务器端没有对应的用户
            printf("ERROR Incorrect username or password!\n");
            continue;
        }
        //服务端有对应的用户，接收到盐值(salt)
        char salt[1024] = {0};
        memcpy(salt,train.data,train.length);
        // printf("salt = %s\n",salt);
        //对salt和明文密码(password)加密,得到密文密码
        char* secret; 
        secret = crypt(password,salt);
        //char secret[1024] = "$6$NlgG8ORy$FklfJZEnQ9xC0io7.iBb9ErSR1IA59tHW4Ao0DXeosQI8Gj8B7IlQEkOv5dzWfYHakCcziz/fX3HD6eFFMSCQ0";
        //将密文密码发送给服务端
        memset(&train,0,sizeof(train_t));
        train.length = strlen(secret);
        memcpy(train.data,secret,train.length);
        send(sockfd,&train.length,sizeof(train.length),0);
        send(sockfd,train.data,train.length,0);
        //接收登录是否成功
        memset(&train,0,sizeof(train_t));
        recvn(sockfd,&train.length,sizeof(train.length));
        recvn(sockfd,train.data,train.length);
        if(memcmp(train.data,"-1",train.length)==0){
            //密码验证失败
            printf("ERROR Incorrect username or password!\n");
            continue;
        }
        if(memcmp(train.data,"0",train.length)==0){
            //密码验证成功
            printf("Success!\n");
            return 0;
        }
    }
    return -1;
}

